package com.orhon.smartcampus.modules.student.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.student.dao.HonorDao;
import com.orhon.smartcampus.modules.student.dao.OperatingDao;
import com.orhon.smartcampus.modules.student.entity.Honor;
import com.orhon.smartcampus.modules.student.entity.Operating;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLHonorInput;
import com.orhon.smartcampus.modules.student.inputMapper.HonorInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddHonorMutation implements DataFetcher {
    @Autowired
    HonorDao honorDao;
    @Autowired
	OperatingDao operatingDao;
    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        String token = tokenService.getSystemToken();
        String username = JwtUtils.getUsername(token);
		Long userId = JwtUtils.getUserId(token);
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLHonorInput input = HonorInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        Honor honor = null;
        
        Operating operating = new Operating();
		String content = "";
        
        if (input.getId() != null){
        	honor = honorDao.get(Honor.class , input.getId());
        	honor.setUpdated_at(new Date());
        	content += "更新荣誉称号:";
        }
        else{
        	honor = new Honor();
        	honor.setCreated_at(new Date());
        	content += "申请荣誉称号:";
        }
        
        if(input.getStudent_id() != null){
        	honor.setStudent_id(input.getStudent_id());
        }
        if(input.getGain_time() != null){
        	honor.setGain_time(input.getGain_time());
        }
        if(input.getExam_name() != null){
        	honor.setExam_name(input.getExam_name());
        	content += input.getExam_name();
        }
        if(input.getRanking() != null){
        	honor.setRanking(input.getRanking());
        }
        if(input.getGrade() != null){
        	honor.setGrade(input.getGrade());
        	content += input.getGrade();
        }
        if(input.getUnits_name() != null){
        	honor.setUnits_name(input.getUnits_name());
        }
        if(input.getStatus() != null){
        	honor.setStatus(input.getStatus());
        	
			if(input.getStatus()==1) {
				operating.setOperate("已审批");
			}
			if(input.getStatus()==2) {
				operating.setOperate("驳回操作");
			}
        }else {
        	honor.setStatus(0);
        	operating.setOperate("提交申请");
        }
        if(input.getRemarks() != null){
        	honor.setRemarks(input.getRemarks());
        }
        honor.setUser_name(username);
        operating.setContent(content);
		operating.setCreated_at(new Date());
		operating.setUser_id(Integer.parseInt(userId+""));
		operating.setUser_name(username);
		operating.setStudent_id(input.getStudent_id());
        
        try{
        	honorDao.saveOrUpdate(honor);
        	operating.setHonor_id(honor.getId());
			operatingDao.saveOrUpdate(operating);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }

        return honor;
    }
}

